<?php

namespace app\rent\controller;

use think\Controller;

class Index extends Controller
{

  public function _initialize()
  {
    parent::_initialize();
    // 实例化模型   
    $this->BusinessModel = model('Business.Business');
    $this->ProductModel = model('Product.Product');
    $this->ProductTypeModel = model('Product.Type');
    $this->CategoryModel = model('Category');
    $this->CollectionModel = model('Business.Collection');
    $this->LeaseModel = model('Lease');

  }
  // 首页
  public function index()
  {
    $recomlist = $this->CategoryModel->where(['flag' => 'recommend'])->order('createtime desc')->limit(5)->select();

    $productlist = $this->ProductModel->where('rentstatus', 'in', ['2', '3'])->where('status', '1')->order('createtime desc')->limit(5)->select();

    $commodity = $this->ProductModel->where('rentstatus', 'in', ['2', '3'])->where('status', '1')->limit(8)->select();

    $data = [
      'recomlist' => $recomlist,
      'productlist' => $productlist,
      'commodity' => $commodity
    ];

    $this->success('', '', $data);
  }

  //分类轮播
  public function categoryrecomlist()
  {
    $data = $this->CategoryModel->order('id desc')->limit(5)->select();
    $this->success('', '', $data);
  }

  // 分类列表
  public function categorylist()
  {
    $page = $this->request->param('page', 1, 'trim');
    $limit = $this->request->param('limit', 5, 'trim');

    $data = $this->CategoryModel->order('id desc')->paginate($limit, true, [
      'page' => $page
    ]);

    if (count($data) > 0) {
      $this->success('', '', $data);
    } else {
      $this->error('暂无数据');
    }
  }

  // 分类详情
  public function categoryinfo()
  {
    $cateid = $this->request->param('cateid', 0, 'trim');
    $busid = $this->request->param('busid', 0, 'trim');

    $info = $this->CategoryModel->find($cateid);

    // 上一篇
    $prev = $this->CategoryModel->where('id', 'lt', $cateid)->order('id DESC')->find();
    // 下一篇
    $next = $this->CategoryModel->where('id', 'gt', $cateid)->find();

    // 查询是否收藏
    $collect = $this->CollectionModel->where(['busid' => $busid, 'cateid' => $cateid])->find();

    if ($collect) {
      $info['collect_text'] = true;
    } else {
      $info['collect_text'] = false;
    }

    $data = [
      'info' => $info,
      'prev' => $prev,
      'next' => $next,
    ];

    $this->success('', '', $data);
  }


  // 收藏、取消收藏文章
  public function collection()
  {
    $cateid = $this->request->param('cateid', 0, 'trim');
    $busid = $this->request->param('busid', 0, 'trim');

    $Businessinfo = $this->BusinessModel->find($busid);
    if (!$Businessinfo) {
      $this->error('当前用户不存在');
    }

    if ($cateid > 0) {
      $Cateinfo = $this->CategoryModel->find($cateid);
      if (!$Cateinfo) {
        $this->error('当前文章不存在');
      }
    }

    $collectinfo = $this->CollectionModel->where(['busid' => $busid, 'cateid' => $cateid])->find();

    // 已经收藏
    if ($collectinfo) {
      $result = $this->CollectionModel->destroy($collectinfo['id']);
      if ($result) {
        $this->success('取消成功');
      } else {
        $this->error($this->CollectionModel->getError());
      }
    } else {
      // 未收藏
      $data = [
        'busid' => $busid,
        'cateid' => $cateid,
      ];

      // 创建验证器对象
      $validator = new \app\common\validate\Rent\Collect();
      // 指定验证场景
      $validator->scene('category');

      // 调用验证器的check方法进行验证
      $result = $validator->check($data);
      if ($result === true) {
        // 通过验证后进行保存
        $iscollect = $this->CollectionModel->save($data);
        if ($iscollect) {
          $this->success('收藏成功', '', ['collect' => $iscollect]);
        } else {
          $this->error($this->CollectionModel->getError());
        }
      } else {
        $this->error($validator->getError());
      }
    }
  }

  //收藏的文章、商品数量
  public function collectnum()
  {
    $mobile = $this->request->param('mobile', '', 'trim');
    $id = $this->request->param('id', 0, 'trim');
    $Businessinfo = $this->BusinessModel->where(['mobile' => $mobile, 'id' => $id])->find();
    if (!$Businessinfo) {
      $this->error('当前用户不存在');
    }
    $data = [];
    $data['articleNum'] = $this->CollectionModel->where(['busid' => $id])->where('cateid', 'not null')->count();
    $data['productNum'] = $this->CollectionModel->where(['busid' => $id])->where('proid', 'not null')->count();
    $data['leaseNum'] = $this->LeaseModel->where(['busid' => $id])->count();

    $this->success('查询成功', '', $data);
  }

  //收藏的文章、商品列表
  public function collectlist()
  {
    $mobile = $this->request->param('mobile', '', 'trim');
    $id = $this->request->param('id', 0, 'trim');
    $collecttype = $this->request->param('collecttype', 'article', 'trim');
    $page = $this->request->param('page', 1, 'trim');
    $limit = $this->request->param('limit', 5, 'trim');

    $Businessinfo = $this->BusinessModel->where(['mobile' => $mobile, 'id' => $id])->find();
    if (!$Businessinfo) {
      $this->error('当前用户不存在');
    }
    $data = [];
    if ($collecttype == 'article') {
      $data = $this->CollectionModel
        ->where(['busid' => $id])
        ->where('cateid', 'not null')
        ->with('category')
        ->order('createtime desc')
        ->paginate($limit, true, ['page' => $page]);

    } else if ($collecttype == 'product') {
      $data = $this->CollectionModel
        ->where(['busid' => $id])
        ->where('proid', 'not null')
        ->with('product')
        ->order('createtime desc')
        ->paginate($limit, true, ['page' => $page]);
    }
    // $this->success('查询成功', '', $data); //

    if (count($data) > 0) {
      $this->success('查询成功', '', $data);
    } else {
      $this->error('暂无数据');
    }
  }

  // 关于我们
  public function about()
  {
    $data = [
      'filings' => config('site.beian'),
      'reactabout' => config('site.reactabout')
    ];

    $this->success('', '', $data);
  }


  //咨询分类轮播
  public function searchrecomlist()
  {
    $data = $this->ProductTypeModel->order('id desc')->limit(5)->select();
    $this->success('', '', $data);
  }
  // 咨询列表
  public function searchlist()
  {
    $page = $this->request->param('page', 1, 'trim');
    $limit = $this->request->param('limit', 5, 'trim');

    $data = $this->ProductModel->order('id desc')->paginate($limit, true, [
      'page' => $page
    ]);

    if (count($data) > 0) {
      $this->success('', '', $data);
    } else {
      $this->error('暂无数据');
    }
  }
}
